<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import _ from 'lodash'
export default {
  name: "App",
  data(){
    return{
      // timmer: null,
      user: null
    }
  },
  mounted(){
    this.user = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null
    let _this = this
  },
  methods: {
    init(){
      const path = ['/login','/loginSSO'];
        if(!path.includes(this.$route.path)) {
      this.user = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null
      if(this.user && this.user.expires_in){
        let nowTime = new Date().getTime()
        let loginTime = +localStorage.getItem('loginTime')
        if((nowTime - loginTime) > (this.user.expires_in * 1000)){
          this.$store.commit("logout", "false");
          this.$router.push({ path: "/login" });
          localStorage.setItem('authToken', '')
          localStorage.setItem('userInfo', '')
        }else if((((this.user.expires_in * 1000) - (nowTime - loginTime)) < 300000) && (((this.user.expires_in * 1000) - (nowTime - loginTime)) > 0)){
          // 五分钟内 获取新的token 替换掉
          let params = {
            scoped: 'all',
            refresh_token: this.user.refresh_token,
            grant_type: 'refresh_token'
          }
          this.$api.oauthToken(params).then(res => {
            if(res && res.code == 200){
              let data = res.data || {}
              localStorage.setItem('loginTime',new Date().getTime())
              localStorage.setItem('authToken',data.access_token)
              localStorage.setItem('userInfo',JSON.stringify(data))
            }
          })
        }
      }
        }

    }
  }
};
</script>

<style> html,
 body {
   width: 100%;
   height: 100%;
   box-sizing: border-box;
   padding: 0px;
   margin: 0px;
   background-color: #EAEDF4;
 }

 #app {
   font-family: PingFang SC, Microsoft YaHei;
   -webkit-font-smoothing: antialiased;
   -moz-osx-font-smoothing: grayscale;
   width: 100%;
   height: 100%;
 }
</style>
